﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.InfoCardMasterKey
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using Microsoft.InfoCards.Diagnostics;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Microsoft.InfoCards
{
  internal class InfoCardMasterKey
  {
    private const byte Marker = 29;
    private const byte Version = 1;
    private Uri m_infoCardId;
    private byte[] m_key;

    public InfoCardMasterKey(Uri infoCardId)
    {
      this.m_infoCardId = infoCardId;
    }

    public InfoCardMasterKey(Uri infoCardId, byte[] key)
    {
      this.m_infoCardId = infoCardId;
      this.m_key = key;
    }

    public static InfoCardMasterKey NewMasterKey(Uri infoCardId)
    {
      InfoCardMasterKey infoCardMasterKey = new InfoCardMasterKey(infoCardId);
      InfoCardTrace.Assert(null == infoCardMasterKey.m_key, "Should be null");
      infoCardMasterKey.m_key = InfoCardMasterKey.GenerateKey();
      return infoCardMasterKey;
    }

    public byte[] Key
    {
      get
      {
        return this.m_key;
      }
    }

    private static byte[] GenerateKey()
    {
      byte[] data = new byte[32];
      new RNGCryptoServiceProvider().GetBytes(data);
      return data;
    }

    public void Get(StoreConnection con)
    {
      InfoCardTrace.Assert((Uri) null != this.m_infoCardId, "populate infocard id before calling Get");
      InfoCardTrace.Assert(con != null, "null connection");
      this.Deserialize((Stream) new MemoryStream(this.GetRow(con, QueryDetails.FullRow).GetDataField()));
    }

    public void Save(StoreConnection con)
    {
      InfoCardTrace.Assert(con != null, "null connection");
      this.ThrowIfNotComplete();
      DataRow row = this.TryGetRow(con, QueryDetails.FullHeader);
      if (row == null)
      {
        row = new DataRow();
        row.ObjectType = 3;
        row.GlobalId = (GlobalId) Guid.NewGuid();
      }
      row.SetIndexValue("ix_parentid", (object) GlobalId.DeriveFrom(this.m_infoCardId.ToString()));
      row.SetIndexValue("ix_masterkey", (object) this.m_key);
      MemoryStream memoryStream = new MemoryStream();
      this.Serialize((Stream) memoryStream);
      row.SetDataField(memoryStream.ToArray());
      con.Save(row);
    }

    public void ThrowIfNotComplete()
    {
      if (!((Uri) null != this.m_infoCardId) || Utility.ArrayIsNullOrEmpty((Array) this.m_key))
        throw InfoCardTrace.ThrowHelperError((Exception) new SerializationIncompleteException(this.GetType()));
    }

    public void Encrypt(PinProtectionHelper pinHelper)
    {
      this.m_key = pinHelper.EncryptMasterKey(this.m_key);
    }

    public void Decrypt(PinProtectionHelper pinHelper)
    {
      this.m_key = pinHelper.DecryptMasterKey(this.m_key);
    }

    public PinProtectionHelper GetPinHelper(string pin)
    {
      return new PinProtectionHelper(pin, this.m_key);
    }

    private void Serialize(Stream stream)
    {
      this.ThrowIfNotComplete();
      BinaryWriter writer = new BinaryWriter(stream, Encoding.Unicode);
      writer.Write((byte) 1);
      Utility.SerializeBytes(writer, this.m_key);
      writer.Write((byte) 29);
    }

    private void Deserialize(Stream stream)
    {
      BinaryReader binaryReader = (BinaryReader) new InfoCardBinaryReader(stream, Encoding.Unicode);
      if ((byte) 1 != binaryReader.ReadByte())
        InfoCardTrace.Assert(false, "Master key version mismatch");
      this.m_key = binaryReader.ReadBytes(binaryReader.ReadInt32());
      if ((byte) 29 != binaryReader.ReadByte())
        InfoCardTrace.Assert(false, "Invalid stream detected");
      this.ThrowIfNotComplete();
    }

    protected DataRow GetRow(StoreConnection con, QueryDetails details)
    {
      DataRow row = this.TryGetRow(con, details);
      InfoCardTrace.Assert(row != null && 3 == row.ObjectType, "invalid service object type");
      return row;
    }

    protected DataRow TryGetRow(StoreConnection con, QueryDetails details)
    {
      InfoCardTrace.Assert((Uri) null != this.m_infoCardId, "populate cardid before retrieving row");
      return con.GetSingleRow(details, new QueryParameter("ix_objecttype", new object[1]
      {
        (object) 3
      }), new QueryParameter("ix_parentid", new object[1]
      {
        (object) GlobalId.DeriveFrom(this.m_infoCardId.ToString())
      }));
    }
  }
}
